package com.qingke.memo.app;

import com.qingke.memo.command.Command;
import com.qingke.memo.factory.CommandFactory;
import com.qingke.memo.factory.CommandFactory.CommandCode;
import com.qingke.memo.pojo.Memo;
import com.qingke.memo.unit.MConsole;

public class MemorandumApplication {
	private static MemorandumApplication instance=null;
	private Memo memo=null;
	private boolean isRun=true;
//	private String promptStr;
	private MemorandumApplication(){
		
	}
	
	public static MemorandumApplication getInstance(){
		if(instance==null){
			instance=new MemorandumApplication();
		}
		return instance;
	}

	public void setMemo(Memo memo) {
		this.memo = memo;
	}

	public void setRun(boolean isRun) {
		this.isRun = isRun;
	}
	

	
	public void run(){
		MConsole.println("Welcome");
		String cmd;
		while(isRun){
			cmd=MConsole.prompt("cmd>").trim().replaceAll(" ","").toUpperCase();
			
			CommandCode cmdCode = null;
			try{
				cmdCode=CommandCode.valueOf(cmd);
			}catch(Exception e){
				MConsole.println("err input,you can input 'help' to look command.");
				
				continue;
			}
			
			CommandFactory factory = CommandFactory.getFactory(memo); 
			Command command = factory.buildCommand(cmdCode);
			if(command!=null){
				command.execute();
				continue;
			}
			MConsole.println("err input,you can input 'help' to look command.");
			
		}
	}
}
